#include "smelter_shuttle.h"
#include "helper.h"
#include "debug.h"
typedef struct
{
    su16 curIndex;
    Helper_EaseInEaseOut_Context EaseSpeed;
    Helper_EaseInEaseOut_Context EaseR;
    Helper_EaseInEaseOut_Context EaseG;
    Helper_EaseInEaseOut_Context EaseB;
} Smelter_Shuttle_Context;

static Smelter_Shuttle_Context context;

void smelter_shuttle_fillDot(Config_Mode_Shuttle *shuttle, su8 index, su8 k)
{
    rgbStrip.colors[index].R = helper_cacl_precentage_denoinator255(context.EaseR.key, k);
    rgbStrip.colors[index].G = helper_cacl_precentage_denoinator255(context.EaseG.key, k);
    rgbStrip.colors[index].B = helper_cacl_precentage_denoinator255(context.EaseB.key, k);
}

void smelter_shuttle_fill_seg(Config_Mode_Shuttle *shuttle, su8 start, su8 totalLen)
{
    su8 middle;
    su16 index = 0;
    su8 onePiece = 0;
    middle = totalLen >> 1;
    if (middle == 0)
    {
        smelter_shuttle_fillDot(shuttle, start, 255);
        return;
    }
    else
    {
        onePiece = 255 / middle;
    }

    for (index = 0; index < middle; index++)
    {
        smelter_shuttle_fillDot(shuttle, (index + start) % gCfgFactoryPtr->enabledDotNumber, index * onePiece);
    }

    for (index = middle; index < totalLen; index++)
    {
        smelter_shuttle_fillDot(shuttle, (index + start) % gCfgFactoryPtr->enabledDotNumber, 255 - ((index - middle) * onePiece));
    }
}

void smelter_shuttle_restart(Config_Mode_Shuttle *shuttle)
{
    context.curIndex = 0;
    context.EaseR.target = shuttle->color.R;
    context.EaseG.target = shuttle->color.G;
    context.EaseB.target = shuttle->color.B;
    context.EaseR.key = shuttle->color.R;
    context.EaseG.key = shuttle->color.G;
    context.EaseB.key = shuttle->color.B;
}

Efct_Stage smelter_shuttle_process(Config_Mode_Shuttle *shuttle)
{
    su8 ii = 0;
    su8 singleSegLen = 0;
    su8 singleSegLeft = 0;
    su16 speedCalResult = 0;

    context.EaseSpeed.target = shuttle->speed;
    context.EaseR.target = shuttle->color.R;
    context.EaseB.target = shuttle->color.B;
    context.EaseG.target = shuttle->color.G;

    helper_easeInEaseOutGenerate(&context.EaseSpeed);
    helper_easeInEaseOutGenerate(&context.EaseR);
    helper_easeInEaseOutGenerate(&context.EaseG);
    helper_easeInEaseOutGenerate(&context.EaseB);

    context.curIndex = (context.curIndex + context.EaseSpeed.key) % (gCfgFactoryPtr->enabledDotNumber << 5);
    speedCalResult = ((context.curIndex >> 5) % gCfgFactoryPtr->enabledDotNumber);

    if (shuttle->segNumber == 0 || shuttle->segNumber > gCfgFactoryPtr->enabledDotNumber)
    {
        smelter_shuttle_fill_seg(shuttle, speedCalResult, gCfgFactoryPtr->enabledDotNumber);
    }
    else
    {
        singleSegLen = gCfgFactoryPtr->enabledDotNumber / shuttle->segNumber;
        singleSegLeft = gCfgFactoryPtr->enabledDotNumber % shuttle->segNumber;

        ii = 0;

        if (singleSegLeft != 0)
        {
            smelter_shuttle_fill_seg(shuttle, speedCalResult, singleSegLen + singleSegLeft);
            ii++;
        }

        for (; ii < shuttle->segNumber; ii++)
        {
            su8 index;
            index = ii * singleSegLen;
            index = (index + speedCalResult) % gCfgFactoryPtr->enabledDotNumber;
            smelter_shuttle_fill_seg(shuttle, index, singleSegLen);
        }
    }
    return E_PROCESS;
}